/*
 * Created by LiuYou on 2020/12/30.
 *
 */

#include "Pcb.hpp"
#include <utility>

ProcessSchedule::Pcb::Pcb(std::string processName, int executeTime, short priority, ProcessState processState)
        : processName(std::move(processName)), executeTime(executeTime), priority(priority),
          processState(processState) {}

ProcessSchedule::Pcb::Pcb(std::string processName, int executeTime, short priority, std::shared_ptr<Pcb> next,
                          ProcessState processState) : processName(std::move(processName)), executeTime(executeTime),
                                                       priority(priority), processState(processState),
                                                       next(std::move(next)) {}

ProcessSchedule::Pcb::~Pcb() = default;

const std::string& ProcessSchedule::Pcb::getProcessName() const {
    return processName;
}

int ProcessSchedule::Pcb::getExecuteTime() const {
    return executeTime;
}

short ProcessSchedule::Pcb::getPriority() const {
    return priority;
}

ProcessSchedule::ProcessState ProcessSchedule::Pcb::getProcessState() const {
    return processState;
}

const std::shared_ptr<ProcessSchedule::Pcb>& ProcessSchedule::Pcb::getNext() const {
    return next;
}

std::string ProcessSchedule::Pcb::toString() {
/*    return "Pcb{processName=" + processName +
           ", executeTime=" + std::to_string(executeTime) +
           ", priority=" + std::to_string(priority) +
           ", processState=" + processName +
           ", next=" + next->getProcessName() + "}";*/

    auto funcProcessState = [](const Pcb& pcb) -> std::string {
        if (pcb.processState == ProcessState::READY) {
            return "ready";
        } else if (pcb.processState == ProcessState::TERMINATED) {
            return "terminated";
        } else {
            return "";
        }
    };
    auto funcNext = [](const Pcb& pcb) -> std::string {
        if (pcb.next == nullptr) {
            return "null";
        } else {
            return pcb.next->getProcessName();
        }
    };
    return "Pcb{processName=" + processName +
           ", executeTime=" + std::to_string(executeTime) +
           ", priority=" + std::to_string(priority) +
           ", processState=" + funcProcessState(*this) +
           ", next=" + funcNext(*this) + "}";
}

void ProcessSchedule::Pcb::setNext(const std::shared_ptr<Pcb>& next) {
    Pcb::next = next;
}

void ProcessSchedule::Pcb::setProcessName(const std::string& processName) {
    Pcb::processName = processName;
}

void ProcessSchedule::Pcb::setExecuteTime(int executeTime) {
    Pcb::executeTime = executeTime;
}

void ProcessSchedule::Pcb::setPriority(short priority) {
    Pcb::priority = priority;
}

void ProcessSchedule::Pcb::setProcessState(ProcessSchedule::ProcessState processState) {
    Pcb::processState = processState;
}


